package czk.concrent.threadpool.timer;

import java.util.concurrent.*;

/**
 * @author chenzhangkun
 * @ClassName SingleFixRateThreadPool
 * @description: TODO
 * @date 2024年12月06日
 */
public class SingleFixRateThreadPool {
	static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

	/**
	 * 全部会加入线程池，但是不会立即执行，而是等待时间间隔，再执行，所以会存在时间间隔的问题
	 * 加入等待队列
	 *
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		scheduledExecutorService.scheduleAtFixedRate(()->task("1"),1,1,TimeUnit.SECONDS);
		TimeUnit.SECONDS.sleep(2);
		scheduledExecutorService.scheduleAtFixedRate(()->task("2"),1,1,TimeUnit.SECONDS);
		TimeUnit.SECONDS.sleep(2);
		scheduledExecutorService.scheduleAtFixedRate(()->task("3"),1,1,TimeUnit.SECONDS);
		TimeUnit.SECONDS.sleep(20);
		while (true){}

	}
	public static  void   task(String content)  {
		try {
			TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
		System.out.println(content);
	}
}
